package work02.stack;

import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;

/**
 * @author sunlggggg
 * @date 2017/4/16
 */
public class StackTest {

    @Test
    public void isEmpty() throws Exception {
        Stack stack = new Stack();
        Assert.assertTrue(stack.isEmpty());
        stack.push(1);
        Assert.assertTrue(!stack.isEmpty());
    }

    @Test
    public void topCase1() throws Exception {
        Stack stack = new Stack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        Assert.assertEquals(stack.top() , 3);
    }

    @Test
    public void topCase2() throws Exception {

        Stack stack = new Stack();
        try {
            stack.top();
            fail("Should have thrown an Exception because Stack is empty!");
        } catch (Exception e) {
            assertThat(e.getMessage(), containsString("Stack is empty"));
        }
    }

    @Test
    public void push() throws Exception {
        Stack stack = new Stack();
        try {
            for (int i = 0; i < 1024 + 1; i++) {
                stack.push(1);
            }
            fail("Should have thrown an Exception because Stack is full!");
        }catch (StackFullException e){
            assertThat(e.getMessage(),containsString("Stack is full"));
        }
    }

    @Test
    public void pop() throws Exception {
        Stack stack = new Stack();
        try {
            for (int i = 0; i < 1024 + 1; i++) {
                stack.pop();
            }
            fail("Should have thrown an Exception because Stack is empty!");
        }catch (StackEmptyException e){
            assertThat(e.getMessage(),containsString("Stack is empty"));
        }
    }



}